/**
 * @Date 2025/2/26 15:11
 * @Created by 衡盛永
 */

package com.sky.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.entity.*;
import com.sky.mapper.AddressBookMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.mapper.UserMapper;
import com.sky.result.PageResult;
import com.sky.service.OrderService;
import com.sky.vo.DishPageVO;
import com.sky.vo.OrderSubmitVO;
import com.sky.vo.OrderVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

/**
 * 订单业务实现类
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private AddressBookMapper addressBookMapper;

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Override
    @Transactional
    public OrderSubmitVO submit(OrdersSubmitDTO ordersSubmitDTO) {
        Orders orders = new Orders();
        BeanUtils.copyProperties(ordersSubmitDTO, orders);
        String orderNumber = UUID.randomUUID().toString().replace("-", "").toUpperCase();
        orders.setNumber(orderNumber);
        orders.setStatus(1);
        orders.setUserId(8L);
        orders.setPayStatus(0);
        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();
        orders.setOrderTime(now);
        // 根据id获取用户信息
        User userById = userMapper.getUserById(8L);
        orders.setPhone(userById.getPhone());
        orders.setUserName(userById.getName());
        // 根据 addressId 获取收货地址信息
        AddressBook addressBook = addressBookMapper.getAddressBookById(ordersSubmitDTO.getAddressBookId());
        orders.setAddress(addressBook.getDetail());
        orders.setConsignee(addressBook.getConsignee());
        // 根据用户 id 获取购物车信息
        List<ShoppingCart> shoppingCart = shoppingCartMapper.list(8L);
        // 新增订单
        orderMapper.saveOrders(orders);
        for (ShoppingCart cart : shoppingCart) {
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setOrderId(orders.getId());
            orderDetail.setName(userById.getName());
            orderDetail.setImage(cart.getImage());
            orderDetail.setAmount(cart.getAmount());
            orderDetail.setNumber(cart.getNumber());
            orderDetail.setDishId(cart.getDishId());
            orderDetail.setDishFlavor(cart.getDishFlavor());
            orderDetail.setSetmealId(cart.getSetmealId());
            orderMapper.saveOrderDetail(orderDetail);
        }
        OrderSubmitVO orderSubmitVO = new OrderSubmitVO();
        orderSubmitVO.setId(orders.getId());
        orderSubmitVO.setOrderTime(now);
        orderSubmitVO.setOrderAmount(ordersSubmitDTO.getAmount());
        orderSubmitVO.setOrderNumber(orderNumber);
        return orderSubmitVO;
    }

    @Override
    public PageResult pageQuery(OrdersPageQueryDTO ordersPageQueryDTO) {
        int page = ordersPageQueryDTO.getPage();
        int pageSize = ordersPageQueryDTO.getPageSize();
        PageHelper.startPage(page, pageSize);
        ordersPageQueryDTO.setUserId(8L);
        List<OrderVO> orderVOList = orderMapper.pageQueryList(ordersPageQueryDTO);
        for (OrderVO orderVO : orderVOList) {
            orderVO.setOrderDishes("菜品信息" + orderVO.getId());
        }
        PageInfo<OrderVO> dishPageVOPageInfo = new PageInfo<>(orderVOList);
        PageResult pageResult = new PageResult();
        pageResult.setTotal(dishPageVOPageInfo.getTotal());
        pageResult.setRecords(dishPageVOPageInfo.getList());
        return pageResult;
    }
}
